package com.moliku.insurance.process.config;

import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * 自定义 注入 Spring Bean命名生成器
 *
 * @author Mr.Liu
 * @since 2020-02-01
 */
public class SpringAnnotationBeanNameGenerator extends AnnotationBeanNameGenerator {

    @Override
    protected @NotNull String buildDefaultBeanName(@NotNull BeanDefinition definition) {
        String name = null;
        if (definition instanceof AnnotatedBeanDefinition) {
            String beanName = determineBeanNameFromAnnotation((AnnotatedBeanDefinition) definition);
            if (StringUtils.hasText(beanName)) {
                name = beanName;
            }
        }
        if (StringUtils.hasText(name)) {
            return name;
        }
        return Objects.requireNonNull(definition.getBeanClassName());
    }
}
